/**
 * @(#) ReqTrainingProxy.as
 *
 * @date		2009. 03. 19.
 * @version		1.0
 * @author		Park, Juneho
 * @description 가족정보 Proxy - Proxy
 * 				가족정보를 조회 한다.
 */
package com.skt.common_hr.training.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.training.TrainingFacade;
	import com.skt.common_hr.training.model.vo.ReqTrainingVO;
	import com.skt.common_hr.training.model.vo.TrainingListVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class ReqTrainingProxy extends CommonProxy
	{
		public static const NAME:String = "ReqTrainingProxy";
		
		public var reqTrainingList:ArrayCollection;
		public var resultKey:String = "";
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function ReqTrainingProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		/**
		 * 초기 신청현황 리스트
		 */
		public function loadReqTraningList(pernr:String):void
		{
			var param:Object = new Object();
			param["I_PERNR"] = pernr;
			loadData("Z_HR_0077", param, listResult, listFault); 
		}
		
		
		public function save(table1:Array):void
		{
			var param:Object = new Object();
			param["I_GWAREKEY"]	= "";				//null값을 넘김
			//param["I_HTML"]	= "";
			//param["I_XML"]	= ""; //buildXML();
			var tables:Object  = new Object();
			tables["ITAB"] = table1;
			
			saveData("Z_HR_0078", param, tables, resultSave, faultSave); 
		}
		
		// 반려건에 대한 삭제
		public function deleteTraining(gKeys:Array):void{
			var param:Object = new Object();
			var tables:Object = new Object();
			tables["I_GWAREKEY"] = gKeys;
			
			saveData("Z_HR_0079", param, tables, delResult, delFault); 
		}
		
		
		
		
		
		
		
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 신청현황 리스트 조회 성공
		 */
		public function listResult(event:ResultEvent):void
		{
			if(retCode==0) {  
				reqTrainingList = parseReqTrainingList(event.result.ITAB);
				facade.sendNotification(TrainingFacade.LOAD_REQ_TRAINING_LIST_COMPLETE, this);
			}
			else {
				facade.sendNotification(TrainingFacade.LOAD_REQ_TRAINING_LIST_FAILED, this);
			}
		}
		
		/**
		 * 신청현황 리스트 조회 실패
		 */
		private function listFault(event:FaultEvent):void
		{
			facade.sendNotification(TrainingFacade.LOAD_REQ_TRAINING_LIST_FAILED, this);
		}
		
		
		
		/**
		 *	저장완료 
		 */
		public function resultSave(event:ResultEvent):void
		{
		 	if(retCode==0) {
		 		resultKey = event.result["O_GWAREKEY"];
				facade.sendNotification(TrainingFacade.SAVE_REQ_TRAINING_COMPLETE, this);
			}
			else {
				resultKey = "";
				facade.sendNotification(TrainingFacade.SAVE_REQ_TRAINING_FAILED, this);
			} 
		}
		
		/***
		 * 저장실패
		 * **/
		private function faultSave(event:FaultEvent):void
		{
		 	facade.sendNotification(TrainingFacade.SAVE_REQ_TRAINING_FAILED, this);
		}
		

		
		/**
		 * 삭제성공
		 */
		public function delResult(event:ResultEvent):void
		{
			if(retCode==0) {  
				facade.sendNotification(TrainingFacade.DELETE_REQ_TRAINING_COMPLETE, this);
			}
			else {
				facade.sendNotification(TrainingFacade.DELETE_REQ_TRAINING_FAILED, this);
			}
		}
		
		/**
		 * 삭제실패
		 */
		private function delFault(event:FaultEvent):void
		{
			facade.sendNotification(TrainingFacade.DELETE_REQ_TRAINING_FAILED, this);
		}







		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		private function parseReqTrainingList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:TrainingListVO = new TrainingListVO();
				vo.GWAREKEY	= rec["GWAREKEY"];	//전자결재 key
				vo.PERNR	= StringUtil.trimFront(rec["PERNR"], "0");		//사원번호
				vo.LCODE	= rec["LCODE"];		//교육체계(L) 코드
				vo.LTEXT	= rec["LTEXT"];		//오브젝트 이름
				vo.DCODE	= rec["DCODE"];		//교육(D) 코드
				vo.DTEXT	= rec["DTEXT"];		//오브젝트 이름
				vo.CHASU	= rec["CHASU"];		//차수
				vo.BEGDA	= rec["BEGDA"];		//시작일
				vo.ENDDA	= rec["ENDDA"];		//종료일
				vo.UCODE	= rec["UCODE"];		//교육기관(U) 코드
				vo.UTEXT	= rec["UTEXT"];		//오브젝트 이름
				vo.RCODE	= rec["RCODE"];		//교육장소(R) 코드
				vo.RTEXT	= rec["RTEXT"];		//오브젝트 이름
				vo.TFORM	= rec["TFORM"];		//교육형태
				vo.TFORMT	= rec["TFORMT"];	//교육형태 텍스트
				vo.IDATE	= rec["IDATE"];		// 신청일
				vo.DSTATUS	= rec["DSTATUS"];	//결재진행상태
				vo.DSTATUST	= rec["DSTATUST"];	//처리유형로그
				retArr.addItem(vo);
			}
			return retArr;	
		}
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 가족관계 Value Object
		 */
		private function parseTrainingList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:ReqTrainingVO = new ReqTrainingVO();
				vo.GWAREKEY		= rec["GWAREKEY"];		// Groupware 전자결재 Key
				vo.PERNR		= StringUtil.trimFront(rec["PERNR"], "0");			// 사원 번호
				vo.LCODE		= rec["LCODE"];			//경조 가족관계 텍스트
				vo.LTEXT		= rec["LTEXT"];			// 교육체계(L)코드
				vo.DCODE		= rec["DCODE"];			//
				vo.DTEXT		= rec["DTEXT"];			// 교육(D)코드
				vo.CHASU		= rec["CHASU"];			//
				vo.BEGDA		= rec["BEGDA"];			// 시작일
				vo.ENDDA		= rec["ENDDA"];			// 종료일
				vo.UCODE		= rec["UCODE"];			// 교육기관(U)코드
				vo.UTEXT		= rec["UTEXT"];			// 
				vo.RCODE		= rec["RCODE"];			//  교육장소(R)코드
				vo.RTEXT		= rec["RTEXT"];			//
				vo.TFORM		= rec["TFORM"];			//  교육형태
				vo.TFORMT		= rec["TFORMT"];		//
				vo.DSTATUS		= rec["DSTATUS"];		// 결재진행상태
				vo.DSTATUST		= rec["DSTATUST"];		//
				 
				retArr.addItem(vo);
			}
			return retArr;	
		}	
		
		
		
		private function buildXML():String
		{
			var tmpXML:XML = new XML("<header>"+
						"<line>"+
							"<line_author></line_author>"+
							"<line_reviewer seq='1'></line_reviewer>"+
							"<line_reviewer seq='2'></line_reviewer>"+
							"<line_approver></line_approver>"+
							"<line_docharger></line_docharger>"+
						"</line>"+
						"<inf>"+
						    "<inf_type>HR</inf_type>"+
						    "<inf_formkind>IF_HR_007</inf_formkind>"+
						    "<inf_keycode>__keycode__</inf_keycode>"+
						    "<inf_subject>교육신청 품의</inf_subject>"+
						    "<inf_createdt></inf_createdt>"+
						    "<inf_htmlnm>__filename__</inf_htmlnm>"+
						"</inf>"+
					"</header>");
			tmpXML.line.line_author = StringUtil.trimFront(Variables.myPernr, "0");
			tmpXML.line.line_approver = Variables.myBossCode;
			//tmpXML.line.line_docharger = Variables.getModuleManager("TRAINING");
			tmpXML.inf.inf_createdt = DateUtil.today();
			
			return "<?xml version='1.0' encoding='utf-8'?>" + tmpXML.toXMLString();
		}
		
	}
}